using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;

namespace svs.Mobile.BeyondMedia.Controls
{
	public partial class CategoryPanel : UserControl
	{
		public delegate void CategorySelectedEvent(FeedCategory category);

		public event CategorySelectedEvent CategorySelected;

		private MainMenu _ParentMenu;
		private MainMenu _CategoryMenu;

		public CategoryPanel()
		{
			InitializeComponent();
			Visible = false;
			categories.Select += OnCategorySelected;

			categories.CustomizedFor = KineticCategoryListView.CustomizeFor.CategorySelection;

			this._CategoryMenu = new MainMenu();

			var close = new MenuItem {Text = "Cancel"};
			close.Click += OnCloseClick;

			var ok = new MenuItem {Text = "OK"};
			ok.Click += OnOKClick;

			_CategoryMenu.MenuItems.Add(ok);
			_CategoryMenu.MenuItems.Add(close);
		}

		
		private void OnOKClick(object sender, EventArgs e)
		{
			if(categories.SelectedCategory != null)
				OnCategorySelected(categories.SelectedCategory);
			else
				HideList();
		}

		private void OnCloseClick(object sender, EventArgs e)
		{
			HideList();
		}

		private void OnCategorySelected(FeedCategory category)
		{
			if (CategorySelected != null)
				CategorySelected(category);
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			
		}

		public void ShowList()
		{
			categories.SetCategories(CategoryManager.GetCategoriesForFilter(), Configuration.ActiveFeedCategory);

			_ParentMenu = Master.MasterForm.Menu;
			Master.MasterForm.Menu = _CategoryMenu;
			categories.Focus();

			Visible = true;
			BringToFront();

		}

		public void HideList()
		{
			Master.MasterForm.Menu = _ParentMenu;
			Visible = false;
			Parent.Focus();
		}
	}
}